package com.haixiaoke.saas.enterprise.tenant.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.constant.TenantConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.system.framework.security.util.SecurityUtils;
import com.haixiaoke.saas.tenant.domain.ElectronicVisa;
import com.haixiaoke.saas.tenant.domain.Tenant;
import com.haixiaoke.saas.tenant.service.IElectronicVisaService;
import com.haixiaoke.saas.tenant.service.ITenantService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 机构信息Controller
 *
 * @author panda
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/enterprise/tenant/tenant")
public class EnterpriseTenantController extends BaseController {
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IElectronicVisaService electronicVisaService;

    /**
     * 查询机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tenant department) {
        startPage();
        List<Tenant> list = tenantService.selectDepartmentList(department);
        return getDataTable(list);
    }

    /**
     * 导出机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tenant department) {
        List<Tenant> list = tenantService.selectDepartmentList(department);
        ExcelUtil<Tenant> util = new ExcelUtil<Tenant>(Tenant.class);
        util.exportExcel(response, list, "机构信息数据");
    }

    /**
     * 获取机构信息详细信息
     */
    @GetMapping
    public AjaxResult getInfo() {
        return success(tenantService.selectDepartmentByDepId(SecurityUtils.getLoginUser().getTenantId()));
    }

    /**
     * 获取机构信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:query')")
    @GetMapping("/info")
    public AjaxResult getDepartmentInfo() {
        return success(tenantService.selectDepartmentByDepId(SecurityUtils.getLoginUser().getTenantId()));
    }

    /**
     * 新增机构信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Tenant department) {
        return tenantService.insertDepartment(department);
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:edit')")
    @PutMapping
    public AjaxResult edit(Tenant department) {
        department.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        return toAjax(tenantService.updateDepartment(department));
    }

    /**
     * 删除机构信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:remove')")
    @DeleteMapping("/{tenantIds}")
    public AjaxResult remove(@PathVariable String[] tenantIds) {
        return toAjax(tenantService.deleteDepartmentByDepIds(tenantIds));
    }


    /**
     * 获取电子签信息
     */
    @GetMapping("/electronicVisaInfo")
    public AjaxResult getElectronicVisaInfo() {
        return success(tenantService.getElectronicVisaInfo(SecurityUtils.getLoginUser().getTenantId()));
    }

    /**
     * 判断电子签是否认证完成
     * @return
     */
    @GetMapping("/byElectronicVisa")
    public AjaxResult byElectronicVisa() {
        ElectronicVisa electronicVisa = electronicVisaService.selectElectronicVisaByTenantId(SecurityUtils.getLoginUser().getTenantId());
        if (ObjectUtil.isEmpty(electronicVisa) ||
                StrUtil.equals(TenantConstants.AUTH_TYPE_NO, electronicVisa.getAuthType())) {
            return success(true);
        }
        return success(false);
    }

    /**
     * 电子签授权认证
     */
    @PutMapping("/sendSmsAuth")
    @RepeatSubmit
    public AjaxResult sendSmsAuth() {
        return electronicVisaService.sendSmsAuth(SecurityUtils.getLoginUser().getTenantId());
    }
}
